﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IFTRUE.FMS.Web.Ashx
{
    /// <summary>
    /// UserManage 的摘要说明
    /// </summary>
    public class UserManage : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //安全验证
            string strMsg = "";
            if (!AshxCheck.Check(context, ref strMsg))
            {
                context.Response.Write(SysMsg.GetCommonMsg(strMsg));
                return;
            }

            //新增
            if (context.Request["type"] == "save")
            {
                if (string.IsNullOrEmpty(context.Request["hidaccount"]))
                {
                    //输入内容验证
                    if (string.IsNullOrEmpty(context.Request["account"]))
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("请输入用户帐号"));
                        return;
                    }
                    if (string.IsNullOrEmpty(context.Request["password"]))
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("请输入密码"));
                        return;
                    }
                    if (string.IsNullOrEmpty(context.Request["name"]))
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("请输入用户名称"));
                        return;
                    }

                    BLL.UserInfo userInfoBLL = new BLL.UserInfo();
                    //判断帐号是否已存在
                    if (userInfoBLL.Exists(context.Request["account"].Trim()))
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("该帐号已存在，请更换帐号"));
                        return;
                    }

                    Model.UserInfo userInfoModel = new Model.UserInfo();
                    userInfoModel.Account = context.Request["account"].Trim();
                    userInfoModel.CreateDate = DateTime.Now;
                    userInfoModel.Folder = context.Request["folder"];
                    userInfoModel.IsUse = (context.Request["isuse"] == "on") ? true : false;
                    userInfoModel.Name = context.Request["name"].Trim();
                    userInfoModel.Password = Helper.Encrypt.MD5(context.Request["password"].Trim());
                    userInfoModel.Phone = context.Request["phone"];
                    bool bl = userInfoBLL.Add(userInfoModel);
                    new BLL.Log().Add(bl?2:3, "用户管理", string.Format("创建用户（{0}）{1}", userInfoModel.Account,bl?"成功":"失败"));
                    if (bl)
                    {
                        context.Response.Write(SysMsg.GetSuccessMsg("UserManage", "closeCurrent"));
                        return;
                    }
                    else
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("操作失败，请联系管理员"));
                        return;
                    }
                }
                //编辑
                else
                {
                    if (string.IsNullOrEmpty(context.Request["name"]))
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("请输入用户名称"));
                        return;
                    }

                    BLL.UserInfo userInfoBLL = new BLL.UserInfo();
                    Model.UserInfo userInfoModel = new Model.UserInfo();
                    userInfoModel.Account = context.Request["hidaccount"].Trim();
                    userInfoModel.Folder = context.Request["folder"];
                    userInfoModel.IsUse = (context.Request["isuse"] == "on") ? true : false;
                    userInfoModel.Name = context.Request["name"].Trim();
                    userInfoModel.Phone = context.Request["phone"];
                    bool bl = userInfoBLL.Update(userInfoModel);
                    new BLL.Log().Add(bl ? 2 : 3, "用户管理", string.Format("编辑用户（{0}）{1}", userInfoModel.Account, bl ? "成功" : "失败"));
                    if (bl)
                    {
                        //context.Response.Write(SysMsg.GetSuccessMsg("UserManage", "closeCurrent"));
                        context.Response.Write("{\"statusCode\":\"200\",\"message\":\"操作成功\", \"navTabId\":\"UserManage\", \"forwardUrl\":\"\", \"callbackType\":\"closeCurrent\"}");
                        return;
                    }
                    else
                    {
                        context.Response.Write(SysMsg.GetCommonMsg("操作失败，请联系管理员"));
                        return;
                    }
                }
            }
            

            //启用
            if (context.Request["type"] == "open")
            {
                Model.UserInfo userInfoModel = new Model.UserInfo();
                BLL.UserInfo userInfoBLL = new BLL.UserInfo();
                string strId = context.Request["ids"];
                bool bl = true;
                if (!string.IsNullOrEmpty(strId))
                {
                    userInfoModel.Account = strId;
                    userInfoModel.IsUse = true;
                   bl= userInfoBLL.Update(userInfoModel, "IsUse", typeof(bool));
                }

                new BLL.Log().Add(bl ? 2 : 3, "用户管理", string.Format("启用用户（{0}）{1}", userInfoModel.Account, bl ? "成功" : "失败"));
                if (bl)
                {
                    context.Response.Write(SysMsg.GetSuccessMsg("UserManage", "forward"));
                    return;
                }
                else
                {
                    context.Response.Write(SysMsg.GetCommonMsg("操作失败，请联系管理员"));
                    return;
                }
            }

            //禁用
            if (context.Request["type"] == "stop")
            {
                Model.UserInfo userInfoModel = new Model.UserInfo();
                BLL.UserInfo userInfoBLL = new BLL.UserInfo();
                string strId = context.Request["ids"];
                if (strId == "admin")
                {
                    context.Response.Write(SysMsg.GetCommonMsg("管理员帐号不可以禁用"));
                    return;
                }

                bool bl = true;
                if (!string.IsNullOrEmpty(strId))
                {
                    userInfoModel.Account = strId;
                    userInfoModel.IsUse = false;
                    userInfoBLL.Update(userInfoModel, "IsUse", typeof(bool));
                }
                new BLL.Log().Add(bl ? 2 : 3, "用户管理", string.Format("禁用用户（{0}）{1}", userInfoModel.Account, bl ? "成功" : "失败"));
                if (bl)
                {
                    context.Response.Write(SysMsg.GetSuccessMsg("UserManage", "forward"));
                    return;
                }
                else
                {
                    context.Response.Write(SysMsg.GetCommonMsg("操作失败，请联系管理员"));
                    return;
                }
            }

            //重置密码
            if (context.Request["type"] == "reset")
            {
                Model.UserInfo userInfoModel = new Model.UserInfo();
                BLL.UserInfo userInfoBLL = new BLL.UserInfo();
                string strId = context.Request["ids"];
                if (!string.IsNullOrEmpty(strId))
                {
                    userInfoModel.Account = strId;
                    string str = new Random().Next(100000, 999999).ToString();
                    userInfoModel.Password = Helper.Encrypt.MD5(str);
                    if (userInfoBLL.Update(userInfoModel, "Password", typeof(string)))
                    {
                        new BLL.Log().Add(2, "用户管理", string.Format("重置用户密码（{0}）成功}", userInfoModel.Account));
                        context.Response.Write(SysMsg.GetSuccessMsg("操作成功，新密码为：" + str, "UserManage", "forward"));
                        return;
                    }
                }
                new BLL.Log().Add(3, "用户管理", string.Format("重置用户密码（{0}）失败}", userInfoModel.Account));
                context.Response.Write(SysMsg.GetCommonMsg("操作失败，请联系管理员"));
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}